package com.bobi.tfca.ui.adapter

import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bobi.tfca.databinding.ItemEntryApplyBinding
import com.bobi.tfca.network.response.AuditResponse
import com.bobi.tfca.util.LogUtil

class EntryApplyAdapter(
    var auditList: ArrayList<AuditResponse>
) :
    RecyclerView.Adapter<EntryApplyAdapter.AuditViewHolder>() {

    // 详情事件
    private var mOnDetailsClickListener: OnItemClickListener? = null

    // 通过按钮时间
    private var mOnPassBtClickListener: OnButtonClickListener? = null

    // 拒绝按钮事件
    private var mOnRefuseBtClickListener: OnButtonClickListener? = null

    interface OnItemClickListener {
        fun onItemClick(position: Int)
    }

    interface OnButtonClickListener {
        fun onMultiBtClick(position: Int)
    }

    fun setOnDetailsClickListener(onItemClickListener: OnItemClickListener?) {
        mOnDetailsClickListener = onItemClickListener
    }

    fun setOnPassBtClickListener(onButtonClickListener: OnButtonClickListener?) {
        mOnPassBtClickListener = onButtonClickListener
    }

    fun setOnRefuseBtClickListener(onButtonClickListener: OnButtonClickListener?) {
        mOnRefuseBtClickListener = onButtonClickListener
    }

    fun setItemList(memberList: ArrayList<AuditResponse>) {
        this.auditList = memberList
    }

    inner class AuditViewHolder(private val binding: ItemEntryApplyBinding) :
        RecyclerView.ViewHolder(binding.root) {

        fun bind(audit: AuditResponse, position: Int) {
            binding.tvInviteName.text = audit.itemName
            binding.tvAuditor.text = audit.auditorName
            binding.tvApplyTime.text = audit.applyTime
            binding.llApplyContent.setOnClickListener {
                mOnDetailsClickListener?.onItemClick(position)
            }
            val auditStatus = audit.auditStatus
            LogUtil.i("auditStatus---" + auditStatus)
            if ("未审核".equals(auditStatus)) {
                binding.llBtPanel.visibility = View.VISIBLE
                binding.tvPassOrRefuse.visibility = View.GONE
                binding.btInviteAccept.setOnClickListener {
                    mOnPassBtClickListener?.onMultiBtClick(position)
                }
                binding.btInviteReject.setOnClickListener {
                    mOnRefuseBtClickListener?.onMultiBtClick(
                        position
                    )
                }
            } else if ("通过".equals(auditStatus)) {
                binding.llBtPanel.visibility = View.GONE
                binding.tvPassOrRefuse.visibility = View.VISIBLE
                binding.tvPassOrRefuse.setTextColor(Color.GREEN)
                binding.tvPassOrRefuse.text = "通过"
            } else if ("拒绝".equals(auditStatus)) {
                binding.llBtPanel.visibility = View.GONE
                binding.tvPassOrRefuse.visibility = View.VISIBLE
                binding.tvPassOrRefuse.setTextColor(Color.RED)
                binding.tvPassOrRefuse.text = "拒绝"
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AuditViewHolder {
        val itemView =
            ItemEntryApplyBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        val viewHolder = AuditViewHolder(itemView)
        return viewHolder
    }

    override fun getItemCount(): Int = auditList.size
    override fun onBindViewHolder(holder: AuditViewHolder, position: Int) {
        val audit = auditList[position]

        with(holder) {
            bind(audit, position)
        }
    }
}